package com.skycode7.servlet;

import com.skycode7.bean.NewsBean;
import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 1.配置网址映射
 * 2.接受前端提交的数据
 * 3.查询数据库（增删改查）
 * 4.页面的跳转或显示（数据给前端）
 */
@WebServlet("/news.html")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//请求参数类型设置
        String sq = req.getParameter("page");//前端的参数都是String类型
        int page = 1;
        if (sq == null && !sq.equals("")) {//传了情况下
            page = Integer.parseInt(sq);//将前端字符串数字强制转化为整形
        }
        NewsDao newsDao = new NewsDao();
        List<NewsBean> list = newsDao.getNewsList(page);//分页查询
        req.setAttribute("list",list);//通过req将list数据传递给前端
        //属于额外知识
        //getRequesDispatcher决定webapp目录下那个文件显示
        req.getRequestDispatcher("WEB-INF/news.jsp").forward(req,resp);
    }
}
